import { forEach } from '@angular/router/src/utils/collection';
import { ModalController, NavParams } from '@ionic/angular';
import { Component, OnInit, Input } from '@angular/core';
import { EventService } from 'src/app/shared/service/event.service';

@Component({
  selector: 'app-modal-multiple-select3',
  templateUrl: './modal-multiple-select3.component.html',
  styleUrls: ['./modal-multiple-select3.component.scss'],
})
export class ModalMultipleSelect3Component implements OnInit {
  @Input() data: any;

  constructor(
    public modalController: ModalController,
    public eventService: EventService,
    private navParams: NavParams,

  ) { }

  ngOnInit() {
    this.data = JSON.parse(JSON.stringify(this.navParams.data.value));
    if (this.data.formType === 'switchButtonFW_3') {
      this.initDataForWwitchButtonFW_3(this.data);
    } else if (this.data.formType === 'modalMultipleSelect3_Auto') {
      this.initDataForModalMultipleSelect3_Auto();
    }

  }
  initDataForWwitchButtonFW_3(formItem) {
    if (formItem.itemList) {
      if (formItem.multiSelect === true) {
        const valueArray: any = [];
        const showValueArray: any = [];
        let value = '';
        let showValue = '';

        formItem.itemList.forEach(e => {
          if (e.check) {
            valueArray.push(e.value);
            showValueArray.push(e.label);
          }
        });
        value = valueArray.join(',');
        showValue = showValueArray.join(',');

        formItem.value = value;
        formItem['showValue'] = showValue;

      } else {
        formItem.itemList.forEach(element => {
          if (element.check) {
            formItem.value = element.value;
            formItem.showValue = element.label;
          }
        });
      }
    }
  }

  changeItemobjValidator(item, type?, i?) {
     // 增加 多选支持
     if (item.multiSelect === true) {
      let value = '';
      const valueArray: any = [];
      const showValueArray: any = [];
  
      item.itemList[i].check = !item.itemList[i].check;
      // 互斥
      if (item.itemList[i].radio === true && item.itemList[i].check === true) {
        item.itemList.forEach(e => {
          if (e.check && e.radio) {

          } else {
            e.check = false;
          }
        });
      } else if (item.itemList[i].check === true) {
        item.itemList.forEach(e => {
          if (e.check && e.radio) {
            e.check = false;
          }
        });
      }


      item.itemList.forEach(e => {
        if (e.check) {
          valueArray.push(e.value);
          showValueArray.push(e.label);
        }
      });
      value = valueArray.join(',');
      item.value = value;
      item['showValue'] = showValueArray.join(',');

    } else {
      if (item.itemList[i].check) {
        return;
      } else {
        item.itemList[i].check = true;
        item.value = item.itemList[i].value;

        item['showValue'] = item.itemList[i].label;
      }
      item.itemList.forEach(element => {
        if (item.itemList[i] !== element) {
          element.check = false;
        }
      });
    }
  }
  initDataForModalMultipleSelect3_Auto() {
  if (this.data && this.data.children && this.data.children.length > 0) {
    this.data.children[0].open = true;
    this.data.current = this.data.children[0];
  }
  this.data.children.forEach(element => {
    if (element.checked) {
      this.data.children[0].open = false;
      element.open = true;
      this.data.current = element;
    }
  });
  // 初始值
  let lastValue: any;
  if (this.data.value instanceof Array) {
    lastValue = this.data.value;

  } else if (this.data.value && typeof (this.data.value) === 'string') {
    lastValue = this.data.value ? this.data.value.split(',') : [];
  } else {
    lastValue = [];
  }
  this.data.value = lastValue;
  if (lastValue.length > 0) {
    // 1
    this.data.children.forEach(element => {

      if (element.children && element.children.length > 0) {
        element.children.forEach(e => {
          if (lastValue.indexOf(e.code) > -1) {
            e.checked = true;
            element.hasValue = true;
            // 2 级可以选择
            e.children.forEach(i => {
              i.checked = true;
            });

          }
          if (e.children && e.children.length > 0) {

            e.children.forEach(i => {
              if (lastValue.indexOf(i.code) > -1) {
                i.checked = true;
                element.hasValue = true;
              }
            });
          }
        });
      }
    });
  } 
}
  // 左边大类点击
  itemLeftClick(it) {
    this.data.children.forEach(element => {
      element.open = false;
    });
    it.open = true;
    this.data.current = it;
  }
  // 2 级可以选择
  chooseSecondAll(it, item) {
    it.checked = !it.checked;

    if (it.checked) {
      if (item.current) {
        item.current.hasValue = true;
      }

      if (typeof item.value === 'string') {
        item.value = item.value.split(',');
      }
      if (item.value.indexOf(it.value) === -1) {
        item.value.push(it.value);
      }

      // 加入大类 清除小类
      it.children.forEach(element => {
        element.checked = true;
        const index = item.value.findIndex(v => (v === element.value));
        if (index > -1) {
          item.value.splice(index, 1);
        }
      });

    } else {
      if (item.current) {
        item.current.hasValue = false;
      }
      item.current.children.forEach(element => {
        if (element.checked) {
          item.current.hasValue = true;
        }
       });

      it.children.forEach(element => {
        element.checked = false;
        //  清除 大类、小类
        const index = item.value.findIndex(v => (v === element.value) || it.value === v);
        if (index > -1) {
          item.value.splice(index, 1);
        }
      });

    }
    this.checkBigVaue(item);
    
    // if (!this.formGroup.value[item.formKey]) {
    // this.formGroup.patchValue({ [item.formKey]: item.value.toString() });
    // }
    // $('#' + it.value).slideToggle("slow");

  }
  checkBigVaue(item) {
    // 遍历当前二级分类是否全选
    if (item.current) {
      let n = 0;
      item.current.children.forEach(element => {
        element.checked ? ++n : '';
      });
      // 全选择 清除 二三级
      if (item.current.children.length === n) {
        item.current.checked = true;
        item.value.push(item.current.value);
        // 清除 二级
        item.current.children.forEach(element => {

          const index = item.value.findIndex(v => (v === element.value));
          if (index > -1) {
            item.value.splice(index, 1);
          }
          // 清除 三级
          this.removeValue(item, element);
        });
      } else {
        item.current.checked = false;
        // 清除一级分类value
        const index = item.value.findIndex(v => (v === item.current.value));
        if (index > -1) {
          item.value.splice(index, 1);
        }
        // 遍历 二 三级
        item.current.children.forEach(element => {
          if (element.checked) {
            if (item.value.findIndex(v => (v === element.value)) === -1) {
              item.value.push(element.value);
            }

          } else {
            element.children.forEach(e => {
              if (e.checked && item.value.findIndex(v => (v === e.value)) === -1) {
                item.value.push(e.value);
              }
            });
          }

        });
      }
    }
    console.log(item.value);
  }
// 2 级可以选择
  tagClick1(tag, it, item) {
    tag.checked = !tag.checked;
    let i = 0;
    it.children.forEach(element => {
      element.checked ? ++i : '';
    });
    i === it.children.length ? it.checked = true : it.checked = false;
    if (it.checked) {
      if (item.current) {
        item.current.hasValue = true;
      }
      if (typeof item.value === 'string') {
        item.value = item.value.split(',');
      }
      // 加入大类 清除小类
      item.value.push(it.value);
      it.children.forEach(element => {

        const index = item.value.findIndex(v => (v === element.value));
        if (index > -1) {
          item.value.splice(index, 1);
        }

      });
    } else {
      it.children.forEach(element => {
        // 先清除
        if (item.current) {
          item.current.hasValue = false;
        }
        const index = item.value.findIndex(v => (v === element.value || v === it.value));
        if (index > -1) {
          item.value.splice(index, 1);
        }

      });

      // 再加入
      it.children.forEach(element => {
        if (element.checked) {
          if (item.current) {
            item.current.hasValue = true;
          }
          item.value.push(element.value);
        }
      });
    }
    this.checkBigVaue(item);
    // this.formGroup.patchValue({ [item.formKey]: item.value.toString() });
  }
  removeValue(item, it) {
    it.children.forEach(e => {
      const index = item.value.findIndex(v => (v === e.value));
      if (index > -1) {
        item.value.splice(index, 1);

      }
    });
  }
  tagClick(tag, index) {
    tag.checked = !tag.checked;
    if (tag.checked) {
      this.data.current.hasValue = true;
      if (typeof this.data.value === 'string') {
        this.data.value = this.data.value.split(',');
      }
      const index = this.data.value.findIndex(v => (v === tag.value));
      if (index === -1) {
        this.data.value.push(tag.value);
      }

    } else {
      const index = this.data.value.findIndex(v => (v === tag.value));
      if (index > -1) {
        this.data.value.splice(index, 1);

      }
      let hasValue = false;
      this.data.current.children.forEach(element => {
        if (element.checked) {
          hasValue = true;
        }
        if (element.children && element.children.length > 0) {
          element.children.forEach(e => {
            if (e.checked) {
              hasValue = true;
            }
          });
        }
      });

      this.data.current.hasValue = hasValue;
    }
    this.checkBigVaue(this.data);
    console.log(this.data.value);
  }

  resetClick() {
    if (this.data.formType === 'switchButtonFW_3') {
      this.data.itemList.forEach(element => {
        element.checked = false;
        element.hasValue = false;
        this.data.value = '';
        this.data.showValue = '';
      });

    } else if (this.data.formType === 'modalMultipleSelect3_Auto') {
      this.data.value = '';
      this.data.showValue = '';
      this.data.children.forEach(element => {
        element.checked = false;
        element.hasValue = false;
        if (element.children && element.children.length > 0) {
          element.children.forEach(e => {
            e.checked = false;
            if (e.children && e.children.length > 0) {
              e.children.forEach(i => {
                i.checked = false;

              });
            }
          });
        }
      });
    }
  }
  sureClick() {
    if (this.data.formType === 'switchButtonFW_3') {
      console.log('showValue', this.data.showValue);
      console.log('Value', this.data.value);
      this.modalController.dismiss(this.data);
    } else if (this.data.formType === 'modalMultipleSelect3_Auto') {
      const valueArray: any = [];
      this.data.children.forEach(element => {

        if (element.children && element.children.length > 0 && element.hasValue) {
          element.children.forEach(e => {
            if (e.checked) {
              // 2级
              valueArray.push(e);
            } else {
              if (e.children && e.children.length > 0) {

                e.children.forEach(i => {
                  if (i.checked) {
                    // 3级
                    valueArray.push(i);
                  }
                });
              }
            }
          });
        }
      });

      console.log('valueArray', valueArray);

      this.modalController.dismiss(valueArray);
    }

  }
  onCancelClick(e) {
    e.stopPropagation();
  }
  dismissClick() {
    this.modalController.dismiss();
  }

}
